package com.ymatou.iapi.productquery.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productprice.parameter.ProductAllPriceBean;
import com.ymatou.iapi.productquery.parameter.ProductDetail.ProductInDetailBean;
import com.ymatou.iapi.productquery.service.GetProductDetailListByProductIdListCall;
import com.ymatou.iapi.productquery.service.GetProductDetailListByTradeIsolationCall;
import com.ymatou.iapi.sellerproduct.service.BatchDeleteProductsCall;
import com.ymatou.iapi.sellerproduct.service.ChangeNewProductInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ActivityPoolService;
import com.ymttest.business.service.AdminCallService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_GetProductDetailListByProductIdList {
	private static GetProductDetailListByProductIdListCall getProductDetailListByProductIdListCall = new GetProductDetailListByProductIdListCall();
	private static GetProductDetailListByTradeIsolationCall getProductDetailListByTradeIsolationCall = new GetProductDetailListByTradeIsolationCall();
	
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static String sellerName = EnvSetup.getData("productpriceSellerName");	
	
	private static ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("非交易隔离查询接口,获取商品详细信息");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductDetailListByProductIdListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	

	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_001() {
		Logger.start(true,"普通单规格商品，普通单规格直播商品 查询");
		try {
			//发布商品
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc2.json");
			JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));
			String productId = SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
			
			List<String> prodList = Arrays.asList(productId);
			
			YMTDateUtil.waitTime(3);
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
			//================================================================================
			//加入直播
			int liveId = LiveCallService.createLiveInProgress(sellerName, Integer.valueOf(sellerId)).getJSONObject("Data").getInt("ActivityId");
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,liveId,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,liveId,true);
			
			
			//================================================================================
			//结束直播
			LiveCallService.closeLive(liveId, Integer.valueOf(sellerId));
			
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
			//================================================================================
			//加入未开始直播
			liveId = LiveCallService.createLiveBeginInFuture(sellerName, Integer.valueOf(sellerId)).getJSONObject("Data").getInt("ActivityId");
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_002() {
		Logger.start(true,"普通多规格商品,普通多规格直播商品 查询");
		try {
			String productId = ProductQueryCallService.getNewProduct(sellerId, sellerName);
				
			List<String> prodList = Arrays.asList(productId);
			
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
			//================================================================================
			//加入直播
			int liveId = LiveCallService.createLiveInProgress(sellerName, Integer.valueOf(sellerId)).getJSONObject("Data").getInt("ActivityId");
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,liveId,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,liveId,true);
			
			//================================================================================
			//暂停销售
			SellerProductCallService.suspendSaleProductInLive(sellerId, liveId, productId);
			
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
		}catch(Exception e ){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_003() {
		Logger.start(true,"普通活动商品查询");
		
		int activityId = 0;
		try {
			String productId = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndNoIsolation(sellerId).getProductId();
			
			List<String> prodList = Arrays.asList(productId);
			
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			activityId = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//================================================================================
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,inaid,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,inaid,0,0,true);
			
			
			//结束活动
			//activityProductsWapper.update(inaid, MapUtil.hashMap("end",YMTDateUtil.getBeforeOrNextDate(-100))); 
			AdminCallService.closeProductInActivity(String.valueOf(inaid));
			
			//================================================================================
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
		}catch(Exception e){
			Logger.fail(e);
		}finally{
			if (activityId>0){ActivityPoolService.removeActivityIdFromPool(activityId);}
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_004() {
		Logger.start(true,"交易隔离活动商品查询");
		int activityId = 0;
		try {
			String productId = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndIsolation(sellerId).getProductId();
			//String productId = "191d3725-9f21-4f92-bc9e-8d8f41d0aadd";
			List<String> prodList = Arrays.asList(productId);						
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			activityId = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//================================================================================
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,inaid,0,0,true);
			
			//结束活动
			//activityProductsWapper.update(inaid, MapUtil.hashMap("end",YMTDateUtil.getBeforeOrNextDate(-100))); 
			AdminCallService.closeProductInActivity(String.valueOf(inaid));
			
			//================================================================================
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
		}catch(Exception e){
			Logger.fail(e);
		}finally{
			if (activityId>0){ActivityPoolService.removeActivityIdFromPool(activityId);}
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_005() {
		Logger.start(true,"活动直播商品查询");
		try {
			String productId = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndNoIsolation(sellerId).getProductId();
			List<String> prodList = Arrays.asList(productId);											
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			
			//================================================================================
			//加入直播
			int liveId = LiveCallService.createLiveInProgress(sellerName, Integer.valueOf(sellerId)).getJSONObject("Data").getInt("ActivityId");
			SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			
			//================================================================================
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,inaid,0,liveId,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,inaid,0,liveId,true);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_006() {
		Logger.start(true,"普通多规格商品，增加规格");
		try {
			//发布商品
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc10.json");	
			JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));

			//call&获取返回报文
			JSONObject result = SellerProductCallService.addNewProduct(addProductBean);
			String productId = result.getJSONObject("Data").getString("ProductID");
			List<String> prodList = Arrays.asList(productId);	
			
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
			
			String fileName = "changenewproductinfocalltpl_003.json";
			new ChangeNewProductInfoCall().callAndGetReturnData(fileName,MapUtil.hashMap("LoginID", sellerName, "UserID", sellerId,
																"Product->sProductId",productId,"ProductID",productId));

			
			YMTDateUtil.waitTime(5);
			
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
			

		}catch(Exception e){
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_007() {
		Logger.start(true,"普通下一场会员活动商品查询");
		int activityId = 0;
		try {
			String productId = ProductPriceCallService.getProductIdWithAllPrice();
			
			ProductPriceCallService.getProdPriceWithActivityInFuture(sellerId,productId,MapUtil.hashMap("ActivityTemplateId", 1));
			
			if (YmtMongoDriverFactroy.IsNewJavaMongoOpen){
				new ActivityProductsWapper().update(productId, MapUtil.hashMap("nbuyer",true,"isolation",false));
			}
			
			
			List<String> prodList = Arrays.asList(productId);
			
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			activityId = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//================================================================================
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,inaid,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,inaid,0,true);
			
			
			//结束活动
			//activityProductsWapper.update(inaid, MapUtil.hashMap("end",YMTDateUtil.getBeforeOrNextDate(1))); 
			AdminCallService.closeProductInActivity(String.valueOf(inaid));
			
			//================================================================================
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
			
		}catch(Exception e){
			Logger.fail(e);
		}finally{
			if (activityId>0){ActivityPoolService.removeActivityIdFromPool(activityId);}
		}
	}
	
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_008() {
		Logger.start(true,"交易隔离下一场活动商品查询");
		int activityId = 0;
		try {
			String productId = ProductPriceCallService.getProductIdWithAllPrice();
			
			ProductPriceCallService.getProdPriceWithActivityInFuture(sellerId,productId,MapUtil.hashMap("ActivityTemplateId", 1));
			
			if (YmtMongoDriverFactroy.IsNewJavaMongoOpen){
				new ActivityProductsWapper().update(productId, MapUtil.hashMap("nbuyer",true,"isolation",true));
			}
			
			
			List<String> prodList = Arrays.asList(productId);
			
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			
			activityId = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			//================================================================================
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,inaid,0,true);
			
			
			
			//结束活动
			//activityProductsWapper.update(inaid, MapUtil.hashMap("end",YMTDateUtil.getBeforeOrNextDate(1))); 
			AdminCallService.closeProductInActivity(String.valueOf(inaid));
			
			//================================================================================
			//check GetProductDetailListByProductIdListCall
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductDetailListByTradeIsolationCall
			checkByProductId(prodList,0,0,0,true);
			
		}catch(Exception e){
			Logger.fail(e);
		}finally{
			if (activityId>0){ActivityPoolService.removeActivityIdFromPool(activityId);}
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_009() {
		Logger.start(true,"查询商品删除");
		try {
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc2.json");
			JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));
			String productId = SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
			
			//删除商品
			new BatchDeleteProductsCall().callAndGetReturnData(MapUtil.hashMap("ProductIds", new JSONArray(Arrays.asList(productId)), "UserID", sellerId));
			
			JSONObject ret1 = getProductDetailListByProductIdListCall.search(Arrays.asList(productId));
			Logger.verifyEquals(false,ret1.isNull("Data"), "删除商品应查出");
			
			
			JSONObject ret2 = getProductDetailListByTradeIsolationCall.search(Arrays.asList(productId));
			Logger.verifyEquals(false,ret2.isNull("Data"), "删除商品应查出");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_010() {
		Logger.start(true, "普通活动商品查询,库存为0");
		int activityId = 0;
		try{
			ProductAllPriceBean productAllPriceBean = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndNoIsolation(sellerId);
			String productId =productAllPriceBean.getProductId();
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			activityId = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			List<String> prodList = Arrays.asList(productId);
			
			//check GetProductInfoByProductId
			checkByProductId(prodList,inaid,0,0,false);
			
			//check GetProductInfoByTradeIsolation
			checkByProductId(prodList,inaid,0,0,true);
			
			//活动库存为0 就移到下一场活动
			ProductPriceCallService.updateActivityStockTo0InMgo(productAllPriceBean, inaid);
			
			//check GetProductInfoByProductId
			checkByProductId(prodList,-1*inaid,0,0,false);
			
			//check GetProductInfoByTradeIsolation
			checkByProductId(prodList,-1*inaid,0,0,true);
			
			
		}catch(Exception e){
			Logger.fail(e);
		}finally{
			if (activityId>0){ActivityPoolService.removeActivityIdFromPool(activityId);}
		}
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetProductDetailListByProductIdList_011() {
		Logger.start(true, "交易隔离活动商品查询,库存为0");
		int activityId = 0;
		try{
			ProductAllPriceBean productAllPriceBean = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndIsolation(sellerId);
			String productId =productAllPriceBean.getProductId();
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			activityId = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			List<String> prodList = Arrays.asList(productId);
			
			//check GetProductInfoByProductId
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductInfoByTradeIsolation
			checkByProductId(prodList,inaid,0,0,true);
			
			//活动库存为0 就移到下一场活动
			ProductPriceCallService.updateActivityStockTo0InMgo(productAllPriceBean, inaid);
			
			//check GetProductInfoByProductId
			checkByProductId(prodList,0,0,0,false);
			
			//check GetProductInfoByTradeIsolation
			checkByProductId(prodList,-1*inaid,0,0,true);
			
			
		}catch(Exception e){
			Logger.fail(e);
		}finally{
			if (activityId>0){ActivityPoolService.removeActivityIdFromPool(activityId);}
		}
	}
	
	
	
	
	
	
	
	
	
	
	public static void checkByProductId(List<String> prodList,int prodInActivityId,int prodInNextActivityId,int liveId,boolean isTradeIsolation) throws Exception{
		//
		List<ProductInDetailBean> tgtProdInDetailBeanList = new ArrayList<>();
		prodList.forEach(ele->{
			try {
				tgtProdInDetailBeanList.add(ProductQueryCallService.getProductInDetailBean(ele, prodInActivityId, prodInNextActivityId, liveId));
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		
		
		JSONObject ret;
		String chkdesc;
		if (isTradeIsolation){
			//交易隔离接口
			ret = getProductDetailListByTradeIsolationCall.search(prodList);
			chkdesc = "检查 GetProductDetailListByTradeIsolation";
		}else{
			
			//普通交易不隔离接口
			ret = getProductDetailListByProductIdListCall.search(prodList);
			chkdesc = "检查 GetProductDetailListByProductIdList";
		}
		
		//返回码检查
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码检查");
		
		
		//返回值取数据
		List<ProductInDetailBean> actProdInDetailBeanList = new ArrayList<>();
		JSONArray prodListAry = ret.getJSONObject("Data").getJSONArray("ProductList");
		for(int i=0;i<prodListAry.length();i++){
			actProdInDetailBeanList.add(ProductQueryCallService.getBeanFromJson(prodListAry.getJSONObject(i),ProductInDetailBean.class));
		}
		
		//比对
		ProductQueryCallService.compareProductInDetailBean(prodList,tgtProdInDetailBeanList, actProdInDetailBeanList,chkdesc);
	}
	
	
	
	
	
	
	
	
	
}
